<?php
require_once("{$CfgPara->root}/model/comment.m.php");
$myComment = new comment_m($CfgPara->database);
if ((isset($InputData['comment_parent_id']) || isset($InputData['comment_parent_type']))
    && (isset($InputData['comment_page']) || isset($InputData['page_change']))
) {
    //获取评论 按页码获取
    if (isset($InputData['comment_page'])) $page_index = $InputData['comment_page'];
    if (isset($InputData['page_change'])) $page_index = $InputData['page_change'];
    if (!isset($workMsg) && $InputData['comment_parent_type'] == 1) {
        //连接作品数据模型
        require_once("{$CfgPara->root}/model/work.m.php");
        //获取作品信息
        $myWork = new work_m($CfgPara->database);
        $commentParentMsg = $myWork->work_msg($InputData['comment_parent_id']);
        $commentParentObj = $myWork;
    } elseif ($InputData['comment_parent_type'] == 1) {
        $commentParentMsg = $workMsg;
        $commentParentObj = $myWork;
    }

    if (!isset($shoeShowMsg) && $InputData['comment_parent_type'] == 2) {
        //连接鞋主秀数据模型
        require_once("{$CfgPara->root}/model/shoe_show.m.php");
        //获取鞋秀信息
        $myShoeShow = new shoe_show_m($CfgPara->database);
        $commentParentMsg = $myShoeShow->show_msg($InputData['comment_parent_id']);
        $commentParentObj = $myShoeShow;
    } elseif ($InputData['comment_parent_type'] == 2) {
        $commentParentMsg = $shoeShowMsg;
        $commentParentObj = $myShoeShow;
    }

    if (isset($InputData['comment_desc']) && $InputData['comment_desc'] == 1)
        $comment_desc = 1;
    else
        $comment_desc = 0;
    if ($commentParentMsg->CommentCount > 0) {
        $commentList = $myComment->comment_list(
            $InputData['comment_parent_id'],
            $InputData['comment_parent_type'],
            $page_index,
            $CfgPara->pagesize,
            $comment_desc);
        $commentCount = $commentParentMsg->CommentCount;
    }

    if (isset($commentList) && is_string($commentList)) //错误信息 直接返回
    {
        echo($commentList);
        goto page_end;
    }
    if (isset($commentCount)) // 如果评论大于0 则计算分页
    {
        if (!isset($myPaginate)) {
            //连接分页类
            require_once("{$CfgPara->root}/class/paginate.class.php");
            $myPaginate = new paginate_c();
        }
        $myPaginate->change_func = "comment_page_change([x])";
        $paginateMsg = $myPaginate->paginate($page_index, ceil($commentCount / $CfgPara->pagesize));
    }

    //连接视图
    if (isset($InputData['comment_page'])) require("{$viewRoot}comment{$viewEx}");
    elseif (isset($InputData['page_change'])) require("{$viewRoot}comment_page{$viewEx}");
} elseif (isset($InputData['comment_edit_view']) &&
    isset($InputData['parent_type']) &&
    isset($InputData['parent_id']) &&
    isset($InputData['reply_id'])
) {
    //进入评论编辑页面
    if ($InputData['parent_type'] == 1) {
        $commentType = '作品';
    } elseif ($InputData['parent_type'] == 2) {
        $commentType = '鞋主秀';
    } else {
        echo('评论主体的类型错误');
        goto page_end;
    }
    if ($InputData['reply_id'] == 0) {
        $commentReply = '';
    } else {
        $commentReply = '的回复';
    }
    if (isset($InputData['comment_at_username'])) {
        $commentEditPla = "回复@{$InputData['comment_at_username']}";
    } else {
        $commentEditPla = '请输入评论';
    }
    require("{$viewRoot}comment_edit{$viewEx}");
} elseif (isset($InputData['parent_type']) &&
    isset($InputData['parent_id']) && isset($InputData['reply_id']) && isset($InputData['comment_text'])
) {
    if (!isset($_SESSION['user_id']) || !is_numeric($_SESSION['user_id'])) {
        echo('请登录');
        goto page_end;
    }
    //提交评论申请 reply_id大于0时 为回复某条评论

    if ($InputData['parent_type'] == 1 || $InputData['parent_type'] == 2) {
        $result = $myComment->comment_save(
            $InputData['parent_id'],
            $InputData['parent_type'],
            $InputData['reply_id'],
            $InputData['comment_text'],
            $InputData['at_username']);
    } else {
        echo('评论类型错误');
        goto page_end;
    }
    echo($result);
    goto page_end;
} elseif (isset($InputData['comment_up'])) {
    //提交评论的点赞
    if (!isset($_SESSION['user_id'])) {
        echo("请登录");
        goto page_end;
    } else {
        if ($InputData['comment_parent_type'] == 1 || $InputData['comment_parent_type'] == 2) {
            $result = $myComment->comment_up($InputData['comment_parent_type'], $InputData['comment_up']);
        } else {
            echo('评论类型错误');
            goto page_end;
        }
        echo($result);
        goto page_end;
    }
} elseif (isset($InputData['comment_id']) && isset($InputData['comment_parent_type']) && isset($InputData['comment_parent_id'])) {
    if (isset($InputData['comment_desc']) && $InputData['comment_desc'] == 1)
        $comment_desc = 1;
    else
        $comment_desc = 0;
    //20170117 新增 查看具体评论+此评论的回复
    if ($InputData['comment_parent_type'] == 1 || $InputData['comment_parent_type'] == 2) {
        /*
        $myComment = new comment_m();
        $myComment->get_new_db($CfgPara->database);
        */
    } else {
        echo('评论类型错误');
        goto page_end;
    }
    if (!isset($InputData['comment_only_reply']))
        $comment = $myComment->comment_msg($InputData['comment_id'], $InputData['comment_parent_type']);
    $replyCount = 0;
    $replyList = $myComment->comment_reply_list(
        $InputData['comment_id'],
        $InputData['comment_parent_type'],
        1,
        $replyCount,
        0,
        $comment_desc);
    //如果当前用户点过赞 则使用实心的赞图标 如果没有则使用镂空的
    $isCommentUp = $myComment->comment_up_check($InputData['comment_id'], $InputData['comment_parent_type']);
    if ($isCommentUp === true) $recommendIconEx = ''; else $recommendIconEx = 'o-';
    if (isset($InputData['comment_only_reply']))
        require("{$viewRoot}comment_show_reply{$viewEx}"); //仅获取全部回复
    else
        require("{$viewRoot}comment_show{$viewEx}");
} elseif (isset($InputData['comment_report'])) {
    //评论举报
    if (!isset($_SESSION['user_id'])) {
        echo("请登录");
        goto page_end;
    } else {
        if ($InputData['comment_parent_type'] == 1 || $InputData['comment_parent_type'] == 2) {
            $result = $myComment->comment_report(
                $InputData['comment_report'], $InputData['comment_parent_type']);
        } else {
            echo('评论类型错误');
            goto page_end;
        }
        echo($result);
        goto page_end;
    }
} elseif (isset($InputData['comment_dele'])) {
    if (!isset($_SESSION['user_id'])) {
        echo("请登录");
        goto page_end;
    }
    if ($InputData['comment_parent_type'] == 1 || $InputData['comment_parent_type'] == 2) {
        $result = $myComment->comment_dele(
            $InputData['comment_dele'],
            $InputData['comment_parent_type'],
            $_SESSION['user_id']);
        echo $result;
        goto page_end;
    } else {
        echo("删除评论时，提交的评论类型错误");
        goto page_end;
    }
}
page_end:;